

import java.util.LinkedList; 
import java.util.List;  

/**
 * This class implements the queue interface and its purpose is
 * to create a backing store for a queue using a linked stack.
 * @author Michael Pogson
 * @version Autumn 2011
 * @param <E> generic place holder for any object.
 */
public class Queue<E> {
  
  /**
   * The backing store used for a queue.
   */
  private final List<E> my_backing_store;
  
  /**
   * A constructor to set up the empty list.
   */
  public Queue() {
    
    my_backing_store = new LinkedList<E>();
  }
  
  /**
   * This method adds the given element to the end of the list.
   * @param the_element This is the element to be added to the end of the list.
   */
  public void enqueue(final E the_element) {
    my_backing_store.add(the_element);
    
  }
  
  /**
   * This method removes an element from the front of the list.
   * @return returns the element at the front of the list.
   * @throws EmptyCollectionException Throws EmptyStackException if the list is empty.
   */
  public E dequeue() {
    
    if (my_backing_store.isEmpty()) {
      throw new IllegalArgumentException("Queue is empty");
    }
    return my_backing_store.remove(0);
  }

  /**
   * Shows the user the first element of the list.
   * @return returns but does not remove the first element of the list.
   * @throws EmptyCollectionException throws EmptyStackException if the list is empty.
   */
  public E peek() throws IllegalArgumentException {
    
    if (my_backing_store.isEmpty()) {
      throw new IllegalArgumentException("Queue is empty");
    }
    return my_backing_store.get(0);
  }

  /**
   * Returns how many elements are currently in the list.
   * @return returns the amount of elements in the list as an int.
   */
  public int size() {
    
    return my_backing_store.size();
  }

  /**
   * returns the state of the list as a boolean.
   * @return returns true if the list is empty false otherwise.
   */
  public boolean isEmpty() {
    
    boolean answer = false;
    
    if (my_backing_store.isEmpty()) {
      
      answer = true;
    }
    
    return answer;
  }
  
  /**
   * This method puts the list into a string format so it can be printed on screen.
   * @return returns a string of the list.
   */
  public String toString() {
    final StringBuilder stb = new StringBuilder();
    stb.append("Front->: ");
    
    for (E i : my_backing_store) {
      
      stb.append(i + " ");
    }
    
    return stb.toString();
  }
}
